<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-row>
          <a-col :span="12" v-if="false">
            <a-form-model-item label="BOM代码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bomCode">
              <a-input v-model="model.bomCode" placeholder="请输入BOM代码"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="false">
            <a-form-model-item label="物料id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialId">
              <a-input v-model="model.materialId" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="产品代码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialCode">
              <a-select
                :dropdownMatchSelectWidth="false"
                :showSearch="true"
                style="width: 100%"
                :value="selectValue"
                :maxTagCount="10"
                @search="searchMaterial"
                @blur="clearMaterialData"
                @select="changeRecord($event)"
                @popupScroll="popupScroll"
                :filter-option="false"
              >
                <a-select-option v-for="(item, index) in materialData" :key="index">
                  <span v-for="(i, index) in materialField" :key="index">
                    {{ item[i] }}
                    <a-divider type="vertical" style="background-color: #0c8fcf"></a-divider>
                  </span>
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="产品名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialName">
              <a-textarea v-model="model.materialName" placeholder="请输入产品名称" disabled></a-textarea>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="规格型号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sap">
              <a-input v-model="model.sap" placeholder="请输入规格型号" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="unit">
              <a-input
                v-model="model.unit"
                placeholder="请输入单位"
                dict-code="hf_material_unit,material_unit_name,id"
                disabled
              ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="物料类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialType">
              <j-dict-select-tag
                v-model="model.materialType"
                placeholder="请输入物料属性"
                dict-code="hf_material_type,material_type_name,id"
                disabled
              ></j-dict-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="物料属性" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialProperty">
              <j-dict-select-tag
                v-model="model.materialProperty"
                placeholder="请输入物料属性"
                dict-code="hf_material_property"
              ></j-dict-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="收料仓库" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="warehouse">
              <j-dict-select-tag
                v-model="model.warehouse"
                placeholder="请输入收料仓库"
                dict-code="hf_warehouse,warehouse_name,id"
              ></j-dict-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="工序" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="step">
              <j-search-select-tag
                v-model="model.step"
                dict="hf_step,name,id"
                placeholder="请选择工序"
              ></j-search-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="false">
            <a-form-model-item label="工序步骤" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="steps">
              <a-input v-model="model.steps"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="标准消耗工时" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="manHour">
              <a-input-number
                v-model="model.manHour"
                :min="0"
                placeholder="请输入标准消耗工时"
                style="width: 100%"
              ></a-input-number>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="标准用量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="number">
              <a-input-number v-model="model.number" placeholder="请输入标准用量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="false">
            <a-form-model-item label="损耗率" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="wastageRate">
              <a-input-number
                v-model="model.wastageRate"
                :min="0"
                :max="100"
                placeholder="请输入损耗率"
                style="width: 100%"
                :formatter="value => value + '%'"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="制单" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="operator">
              <a-input type="list" v-model="model.operator" placeholder="" disabled />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="审核员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="checker">
              <a-input type="list" v-model="model.checker" placeholder="" disabled />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="审核时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="checkTime">
              <j-date
                placeholder=""
                v-model="model.checkTime"
                :show-time="true"
                date-format="YYYY-MM-DD HH:mm:ss"
                style="width: 100%"
                disabled
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" placeholder="请输入备注"></a-textarea>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import { HfIODetailMixin } from '@/mixins/HfIODetailMixin'
import store from '@/store'
import dayjs from 'dayjs'
import HfDictSelect from '@comp/HfManual/HfDictSelectTag.vue'
import HfStepDictSelect from '@views/material/modules/HfStepDictSelectTag.vue'

export default {
  name: 'HfBomModal',
  mixins: [HfIODetailMixin],
  components: {
    HfStepDictSelect,
    HfDictSelect
  },
  data() {
    return {
      title: '操作',
      width: 800,
      visible: false,
      model: {
        operator: store.getters.userInfo.realname,
        documentDate: dayjs().format('YYYY-MM-DD'),
        materialProperty: '自制',
        number: 1
      },
      materialField: ['materialCode', 'materialName', 'sap'],
      labelCol: {
        xs: { span: 24 },
        sm: { span: 8 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },

      confirmLoading: false,
      validatorRules: {
        materialCode: [
          {
            required: true,
            message: '请输入物料代码!',
            trigger: 'blur',
            validator: (rule, value, callback) => {
              if (!this.model.materialCode) {
                callback(new Error('请输入物料代码!'))
              } else {
                callback()
              }
            }
          }
        ],
        number: [{ required: true, message: '请输入数量!' }],
        step: [{ required: true, message: '请输入工序!' }],
        wastageRate: [{ required: true, message: '请输入损耗率!' }]
      },
      url: {
        add: '/material/hfBom/add',
        edit: '/material/hfBom/edit',
        copyAdd: '/material/hfBom/copyAdd'
      },
      copyAddFlag: false
    }
  },

  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },

  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    close() {
      this.selectValue = ''
      this.$emit('close')
      this.visible = false
      this.$refs.form.clearValidate()
    },
    copyAdd(record) {
      this.edit(record)
      this.copyAddFlag = true
    },
    handleOk() {
      let msg = this.title + '成功'
      const that = this

      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          if (this.copyAddFlag) {
            httpurl = this.url.copyAdd
            method = 'post'
          }
          httpAction(httpurl, this.model, method)
            .then(res => {
              if (res.success) {
                that.$message.success(msg)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
              this.selectValue = ''
              this.copyAddFlag = false
            })
        } else {
          return false
        }
      })
    },
    handleCancel() {
      this.close()
    },
    changeRecord(index, record) {
      //找到选中行的数据
      const target = this.materialData[index]
      //找到当前行的数据
      const current = this.model
      Object.keys(target).forEach(key => {
        if (target[key] === null) {
          target[key] = ''
        }
      })
      //设置数据
      current.materialId = target.id
      current.materialCode = target.materialCode
      current.materialName = target.materialName
      current.sap = target.sap
      current.unit = target.unit
      current.materialType = target.materialType
      current.perNumber = target.perNumber
      this.selectValue = target.materialCode
      this.editingKey = ''
    }
  }
}
</script>
